package com.lzh.runke.util;

import com.lzh.runke.core.annotation.date.DateFieldType;
import com.lzh.runke.core.web_base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author LiuZiHao
 * @date 创建时间:2023年06月14日 15:48:44
 */
@Slf4j
public class MyDateUtils {


    /**
     * 获取同比日期
     *
     * @param date          原始日期
     * @param dateFieldType 日期类型
     * @return
     */
    public static Date getYoyDate(Date date, DateFieldType dateFieldType) {
        LocalDateTime dateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        Date yoyDate = null;
        try {
            if (dateFieldType.equals(DateFieldType.DAY)) {
                yoyDate = Date.from(dateTime.minusDays(1L).atZone(ZoneId.systemDefault()).toInstant());
            } else if (dateFieldType.equals(DateFieldType.WEEK)) {
                yoyDate = Date.from(dateTime.minusWeeks(1L).atZone(ZoneId.systemDefault()).toInstant());
            } else if (dateFieldType.equals(DateFieldType.MONTH)) {
                yoyDate = Date.from(dateTime.minusMonths(1L).atZone(ZoneId.systemDefault()).toInstant());
            } else if (dateFieldType.equals(DateFieldType.QUARTER)) {
                yoyDate = Date.from(dateTime.minusMonths(3L).atZone(ZoneId.systemDefault()).toInstant());
            } else if (dateFieldType.equals(DateFieldType.YEAR)) {
                yoyDate = Date.from(dateTime.minusYears(1L).atZone(ZoneId.systemDefault()).toInstant());
            }
        } catch (Exception e) {
            log.error("异常信息:{}", e.getMessage());
            throw new BusinessException("日期格式化异常");
        }
        return yoyDate;
    }
}
